﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BProdProduct
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/7 16:18:01
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion


using KingerRobot.MexicoSailunMDC.Dao;
using System;

namespace KingerRobot.MexicoSailunMDC.SpiService;
internal class BProdProduct : IProdProduct
{
    private readonly static IProdMaterial _ProdMaterial = new BProdMaterial();
    private readonly static IProdSku _ProdSku = new BProdSku();
    private readonly static IRcsEquip _RcsEquip = new BRcsEquip();
    private readonly static IProdSkuProduct _ProdSkuProduct = new BProdSkuProduct();
    private readonly static IMesGreenTireBuilding _MesGreenTireBuilding = new BMesGreenTireBuilding();
    private readonly static IWcsLocSku _WcsLocSku = new BWcsLocSku();
    private readonly static object _Lock = new object();
    private readonly static IMesProduct _MesProduct = new BMesProduct();


    public string GetFilmPrductInfoBySkuId(long skuId)
    {
        var db = Db.Default;
        var result = "";
        var product = db.ProdSkuProducts.Join(db.ProdProducts, f => f.ProductId, s => s.Id, (f, s) => new
        {
            ProdSkuProducts = f,
            ProdProducts = s,
        }).Join(db.MesProductInfos, f => f.ProdProducts.MesId, s => s.Id, (f, s) => new
        {
            f.ProdSkuProducts,
            MesProductions = s,
        }).Where(t => t.ProdSkuProducts.SkuId == skuId).OrderByDescending(t => t.ProdSkuProducts.CreateTime).FirstOrDefault().MesProductions;
        result = product?.Id.ToString() ?? "";
        return result;

        //Where(t => t.Id == skuId).Select(t => t.SkuCode).FirstOrDefault();
    }
    public POProdProduct CreateOrUpdateProduct(ProdProductBean prodProductBean)
    {

        var productCode = prodProductBean.ProductCode;
        var product = GetProductByCode(productCode);
        var MdEquipId = _RcsEquip.GetRcsEquipByCode(prodProductBean.MdEquipCode)?.Id ?? 0L;
        var gtStatus = prodProductBean.QuailtyStatus == 1 ? ConstFields.UnQualifiedProductStatus : ConstFields.QualifiedProductStatus;

        if (product is not null)
        {
            var productInfo = new POProdProduct()
            {
                ProductCode = prodProductBean.ProductCode,
                MdEquipId = MdEquipId,
                MaterialId = prodProductBean.MaterialId,
                MdTime = prodProductBean.MdTime,
                GtStatus = gtStatus,
                TireLock = prodProductBean.TireLock,
                ProductBatch = prodProductBean.ProductBatch,
                UpdateTime = DateTime.Now,
                TbmExpirationTime = prodProductBean.ExpirationTime,
                MesId=prodProductBean.MesId,
            };
            UpdateProduct(productInfo);
            productInfo.Id = product.Id;
            return productInfo;
        }

        POProdProduct pProdProduct = new()
        {
            Id = XID.Nextval,
            ProductCode = prodProductBean.ProductCode,
            MdEquipId = MdEquipId,
            MaterialId = prodProductBean.MaterialId,
            ProductBatch = prodProductBean.ProductBatch,
            MdTime = prodProductBean.MdTime,
            GtStatus = gtStatus,
            TireLock = prodProductBean.TireLock,
            TbmExpirationTime = prodProductBean.ExpirationTime,
            MesId = prodProductBean.MesId,
        };
        var db = Db.Default;
        db.ProdProducts.Insert(pProdProduct);
        return pProdProduct;
    }




    public int UpdateProduct(POProdProduct product)
    {
        var db = Db.Default;
        var result = db.ProdProducts.Where(t => t.ProductCode == product.ProductCode).Update(product);
        return result;
    }

    public ProdProductBindingBean CreateProductBindingInfo(ProductBindingInfoBean productBindingInfoBean)
    {
        WriteLog($"条码{productBindingInfoBean.ProductCode}开始创建绑定关系");
        try
        {
            // 记录胎胚信息
            var productCode = productBindingInfoBean.ProductCode;
            var productBatch = productBindingInfoBean.ProductBatch;

            //这个地方实际上是规格
            var materialCode = productBindingInfoBean.MaterialCode;
            var materialName = productBindingInfoBean.MaterialName;

            var mdTime = productBindingInfoBean.ProductTime ?? DateTime.Now;
            var MdEquipCode = productBindingInfoBean.ProductEquip;
            var tireLock = productBindingInfoBean.TireLock ?? 0;
            var qualityStatus = productBindingInfoBean.QualityStatus ?? 0;

            var material = _ProdMaterial.GetMaterialByCode(materialCode);

            var expirationTime = productBindingInfoBean.ExpirationTime;
            var mesId = productBindingInfoBean.MesId;
            //var mesId = _ProdMaterial.GetMaterialByCode(materialCode)?.MesId;

            //var building = _MesGreenTireBuilding.CreateOrUpdateMesGreenTireBuilding(productCode, mesId, MdEquipCode, mdTime, tireLock, qualityStatus, productBatch);


            // 查找物料
            if (material == null)
            {
                // 添加物料
                lock (_Lock)
                {
                    material = _ProdMaterial.CreateMaterial(materialCode, materialName);
                }
                return null;
            }





            // 添加产品
            var prodProductBean = new ProdProductBean
            {
                ProductBatch = productBatch,
                ProductCode = productCode,
                MaterialId = material?.Id ?? 0,
                MdTime = mdTime,
                MdEquipCode = MdEquipCode,
                QuailtyStatus = qualityStatus,
                TireLock = tireLock,
                ExpirationTime = expirationTime,
                MesId = mesId,
            };

            var product = CreateOrUpdateProduct(prodProductBean);



            // 添加sku
            var skuCode = product?.ProductCode;
            var materialId = product?.MaterialId ?? 0;
            //先删除sku 防止重复上胎
            // _ProdSku.DeleteProSku(skuCode);

            var sku = _ProdSku.CreateProdSku(new()
            {
                SkuCode = skuCode,
                MaterialId = materialId,
                ProductBatch = productBatch,
                MdTime = mdTime,
            });


            // 添加产品 和 sku 关联
            var skuId = sku?.Id ?? 0;
            var productId = product?.Id ?? 0;
            //先删除绑定关系 防止重复上胎
            //_ProdSkuProduct.DeleteProdSkuProductByProductId(productId);
            var skuProduct = _ProdSkuProduct.CreateProdSkuProduct(skuId, productId, materialId, mdTime);

            var result = new ProdProductBindingBean()
            {
                //POMesGreenTireBuilding = building,
                POProdMaterial = material,
                POProdProduct = product,
                POProdSku = sku,
                POProdSkuProduct = skuProduct,
            };
            WriteLog($"条码{productBindingInfoBean.ProductCode}创建绑定关系完成,{result.ToJson()}");
            return result;

        }
        catch (Exception ex)
        {
            WriteLog($"条码{productBindingInfoBean.ProductCode}创建绑定关系异常,{ex.ToString()}");
        }
        return null;
    }

    public ProdProductBindingBean CreateAbnormalProductEmptyBinding(string productCode)
    {
        // 异常数据信息
        if (string.IsNullOrWhiteSpace(productCode))
        {
            // productCode = ConstFields.NoRead;
        }
        var productBatch = string.Empty;
        var materialCode = ConstFields.Abnormal;
        var materialName = ConstFields.Abnormal;


        // 查找物料
        var material = _ProdMaterial.GetMaterialByCode(materialCode);
        if (material == null)
        {
            // 添加物料
            material = _ProdMaterial.CreateMaterial(materialCode, materialName);
        }


        // 查找产品信息【NOREAD 条码是否存在】
        var product = GetProductByCode(productCode);
        if (product == null)
        {
            var mesId = _MesProduct.GetInfoByBarcode(productCode)?.MesId;
            // 添加产品
            product = CreateOrUpdateProduct(new()
            {
                ProductCode = productCode,
                ProductBatch = productBatch,
                MaterialId = material?.Id ?? 0,
                MdTime = DateTime.Now,
                MesId= mesId,
            });
        }
        var productId = product?.Id ?? 0;
        var skuCode = product?.ProductCode;
        var materialId = product?.MaterialId ?? 0;
        var mdTime = product?.MdTime;

        // 添加sku
        var sku = _ProdSku.GetProdSkuBySkuCode(skuCode);
        if (sku == null)
        {
            sku = _ProdSku.CreateProdSku(new()
            {
                SkuCode = skuCode,
                MaterialId = materialId,
                ProductBatch = productBatch,
                MdTime = mdTime,
            });
        }
        var skuId = sku?.Id ?? 0;


        // 添加产品 和 sku 关联
        var skuProduct = _ProdSkuProduct.GetProdSkuProductBySkuId(skuId);
        if (skuProduct == null)
        {
            skuProduct = _ProdSkuProduct.CreateProdSkuProduct(skuId, productId, materialId, mdTime);
        }

        return new ProdProductBindingBean
        {
            POProdMaterial = material,
            POProdProduct = product,
            POProdSku = sku,
            POProdSkuProduct = skuProduct,
        };

    }

    public POProdProduct DeleteProduct(string productCode)
    {
        var db = Db.Default;
        var result = GetProductByCode(productCode);
        if (result != null)
        {
            db.ProdProducts.Remove(result);
            var num = db.SaveChanges();
            if (num == 0)
            {
                return null;
            }
            return result;
        }
        return result;
    }

    public ProdProductBindingBean GetProductBindingInfoByCode(string code)
    {
        ProdProductBindingBean result = null;
        var db = Db.Default;
        var prodProductBindingBeans1 = db.ProdProducts
            .Where(t => t.ProductCode == code)
            .Join(db.ProdSkuProducts, f => f.Id, s => s.ProductId, (f, s) => new { Product = f, SkuProduct = s })
            .Join(db.ProdSkus, f => f.SkuProduct.SkuId, s => s.Id, (f, s) => new { f.Product, f.SkuProduct, Sku = s })
            .Join(db.ProdMaterials, f => f.Product.MaterialId, s => s.Id, (f, s) =>
            new ProdProductBindingBean
            {
                POProdProduct = f.Product,
                POProdSkuProduct = f.SkuProduct,
                POProdSku = f.Sku,
                POProdMaterial = s,
            }).OrderByDescending(t => t.POProdSku.CreateTime);

        //var productsql = prodProductBindingBeans1
        //    .ToQueryString();
        //WriteLog($"打印查找产品sql{productsql}", true);

        var prodProductBindingBeans = prodProductBindingBeans1.ToList();

        //经常出现重复数据无法执行的情况   加个数据过滤   郭海朋  2023年5月10日
        if (prodProductBindingBeans.Count() > 1)
        {
            //有多个SKU信息
            //去任务表里对一下
            foreach (var item in prodProductBindingBeans)
            {
                var count = db.WcsTasks.Where(T => T.SkuId == item.POProdSkuProduct.SkuId).Count();
                if (count > 0)
                {
                    //找到任务对应的数据了  返回这条
                    result = item;
                    break;
                }
            }
            //如果没有当前任务的sku 则取倒序第一个
            result = prodProductBindingBeans.FirstOrDefault();
        }
        else
        {
            result = prodProductBindingBeans.FirstOrDefault();
        }
        return result;
    }



    public List<ProdProductBindingBean> GetProductBindingInfoByCodes(List<string> codes)
    {
        var db = Db.Default;
        var result = db.ProdProducts
            .Where(t => codes.Contains(t.ProductCode))
            .Join(db.ProdSkuProducts, f => f.Id, s => s.ProductId, (f, s) => new { Product = f, SkuProduct = s })
            .Join(db.ProdSkus, f => f.SkuProduct.SkuId, s => s.Id, (f, s) => new { f.Product, f.SkuProduct, Sku = s })
            .Join(db.ProdMaterials, f => f.Product.MaterialId, s => s.Id, (f, s) =>
            new ProdProductBindingBean
            {
                POProdProduct = f.Product,
                POProdSkuProduct = f.SkuProduct,
                POProdSku = f.Sku,
                POProdMaterial = s,
            }).OrderByDescending(t => t.POProdSku.CreateTime).ToList();


        return result;
    }

    public ProdProductBindingBean GetProductBindingInfoBySkuId(long skuId)
    {
        var db = Db.Default;
        var result = db.ProdSkuProducts.Where(t => t.SkuId == skuId)
            .Join(db.ProdProducts, f => f.ProductId, s => s.Id, (f, s) => new { Product = s, SkuProduct = f })
            .Join(db.ProdSkus, f => f.SkuProduct.SkuId, s => s.Id, (f, s) => new { f.Product, f.SkuProduct, Sku = s })
            .Join(db.ProdMaterials, f => f.Product.MaterialId, s => s.Id, (f, s) =>
            new ProdProductBindingBean
            {
                POProdProduct = f.Product,
                POProdSkuProduct = f.SkuProduct,
                POProdSku = f.Sku,
                POProdMaterial = s,
            }).OrderByDescending(t => t.POProdSku.CreateTime).FirstOrDefault();

        return result;
    }

    public long GetSkuIdByCode(string code) => GetProductBindingInfoByCode(code)?.POProdSku?.Id ?? -1;

    public long GetSkuIdByCodeEMSAbnormal(string code)
    {
        var db = Db.Default;
        var result = db.ProdSkus.Where(t => t.SkuCode == code).Select(t => t.Id).FirstOrDefault() ?? 0;
        return result;
    }

    //public string GetCodeBySkuId(long skuId) => GetProductBindingInfoBySkuId(skuId)?.POProdProduct?.ProductCode;

    public string GetCodeBySkuId(long skuId)
    {
        var db = Db.Default;
        var result = db.ProdSkus.Where(t => t.Id == skuId).Select(t => t.SkuCode).FirstOrDefault();
        return result;
    }

    public string GetCodeByLocId(long? locId)
    {
        var skuId = _WcsLocSku.GetWcsLocSkuByLocId(locId)?.SkuId;
        if (skuId is null) return null;
        return GetCodeBySkuId(skuId.GetValueOrDefault());
    }

    public POProdProduct GetProductByCode(string code)
    {
        var db = Db.Default;
        var result = db.ProdProducts.Where(t => t.ProductCode == code)
            .OrderByDescending(t => t.CreateTime).FirstOrDefault();
        return result;
    }
    public List<POProdProduct> GetProductByCodes(List<string> codes)
    {
        var db = Db.Default;
        var result = db.ProdProducts.Where(t => codes.Contains(t.ProductCode)).ToList();
        return result;
    }
    public List<POProdProduct> GetProductByIds(List<long> ids)
    {
        var db = Db.Default;
        var result = db.ProdProducts.Where(t => t.UseFlag == 1 && ids.Contains(t.Id ?? 0)).ToList();
        return result;
    }
    public List<POProdProduct> GetProducts()
    {
        var db = Db.Default;
        var result = db.ProdProducts.ToList();
        return result;
    }
    public List<ProductLocBean> GetProductIsNByLocIds(List<long> locIds)
    {
        var db = Db.Default;

        var locSku = db.WcsLocSkus.Select(t => new
        {
            LocId = t.LocId,
            SkuId = t.SkuId,
        }).ToList();
        var skuIds = locSku.Where(t => locIds.Contains(t.LocId.GetValueOrDefault())).Select(t => t.SkuId).ToList();

        var skuProduct = db.ProdSkuProducts.Where(t => skuIds.Contains(t.SkuId)).ToList();
        var productIds = skuProduct.Select(t => t.ProductId).ToList();

        var product = db.ProdProducts.Where(t => productIds.Contains(t.Id) && t.TireLock == ConstFields.NoLock
        && t.Frozen == ConstFields.UnFrozen).ToList();

        var result = locSku.Join(skuProduct, f => f.SkuId, s => s.SkuId, (f, s) => new
        {
            LocSku = f,
            SkuProduct = s,
        }).Join(product, f => f.SkuProduct.ProductId, s => s.Id, (f, s) => new ProductLocBean
        {
            LocId = f.LocSku.LocId,
            SkuId = f.LocSku.SkuId,
            POProdSkuProduct = f.SkuProduct,
            POProdProduct = s,
        }).ToList();

        return result;
    }






    public List<ProductLocBean> GetWarehousingOutProduct()
    {
        var db = Db.Default;
        var nowTime = DateTime.Now;
        // 质量，冻结
        var result = db.ProdProducts.Join(db.ProdSkuProducts, f => f.Id, s => s.ProductId, (f, s) => new
        {
            ProdSkuProducts = s,
            ProdProducts = f,
        }).Join(db.WcsLocSkus, f => f.ProdSkuProducts.SkuId, s => s.SkuId, (f, s) => new
        {
            WcsLocSkus = s,
            ProdProducts = f.ProdProducts,
            POProdSkuProduct = f.ProdSkuProducts,
        }).Join(db.MesProducts, f => f.ProdProducts.MesId, s => s.Id, (f, s) => new
        {
            WcsLocSkus = f.WcsLocSkus,
            ProdProducts = f.ProdProducts,
            POProdSkuProduct = f.POProdSkuProduct,
            MesProducts = s,
        })
        .Where(t => t.ProdProducts.TireLock == ConstFields.NoLock && t.ProdProducts.TbmExpirationTime > DateTime.Now && t.MesProducts.TbmPs == "0")
        //&& t.ProdProducts.Frozen == ConstFields.UnFrozen && t.ProdProducts.TireLock == ConstFields.UnLock)
        .Join(db.ProdMaterials, f => f.ProdProducts.MaterialId, s => s.Id, (f, s) => new { Product = f, Material = s })
        .Select(t => new ProductLocBean
        {
            LocId = t.Product.WcsLocSkus.LocId,
            SkuId = t.Product.WcsLocSkus.SkuId,
            POProdSkuProduct = t.Product.POProdSkuProduct,
            POProdProduct = t.Product.ProdProducts,
        }).OrderBy(t => t.POProdProduct.MdTime).ToList();

        //var prodMaterial = _ProdMaterial.GetMaterials();
        //var result = product.Join(prodMaterial, f => f.ProdProducts.MaterialId, s => s.Id, (f, s) => new { Product = f, Material = s })
        //  //.Where(t => t.Product.ProdProducts.MdTime?.AddMinutes(t.Material.ExpirMinDuration ?? 0) > nowTime)
        //  .Select(t => new ProductLocBean
        //  {
        //      LocId = t.Product.WcsLocSkus.LocId,
        //      SkuId = t.Product.WcsLocSkus.SkuId,
        //      POProdSkuProduct = t.Product.POProdSkuProduct,
        //      POProdProduct = t.Product.ProdProducts,
        //  }).ToList();
        //.Join(db.ProdMaterials, f => f.ProdProducts.MaterialId, s => s.Id, (f, s) => new { Product = f, Material = s })
        //.Where(t => (t.Product.ProdProducts.MdTime ?? new DateTime()).AddMinutes((t.Material.ExpirMinDuration ?? 0)) > nowTime)
        //.Select(t => new ProductLocBean
        //{
        //    LocId = t.Product.WcsLocSkus.LocId,
        //    SkuId = t.Product.WcsLocSkus.SkuId,
        //    POProdSkuProduct = t.Product.POProdSkuProduct,
        //    POProdProduct = t.Product.ProdProducts,
        //}).ToList();
        //WriteLog($"chukuchanpin{result.ToJson()}", true);
        return result;
    }



    public bool HasProductByCode(string code) => GetProductByCode(code) != null;

    private int DeleteProductBindingInfo(string code)
    {
        var product = DeleteProduct(code);
        if (product != null)
        {
            var proSku = _ProdSku.DeleteProSku(code);
            if (proSku != null)
            {
                var proSkuProduct = _ProdSkuProduct.DeleteProdSkuProduct(proSku.Id);
                if (proSkuProduct != null)
                {
                    return 1;
                }
            }
        }

        return 0;
    }


    public int UpdateProductFrozen(ProductFrozenBean productFrozenBean)
    {
        var db = Db.Default;

        var barCode = productFrozenBean.Barcode;
        var product = db.MesProductFrozens.Where(t => t.Barcode == barCode).FirstOrDefault();

        var result = 0;
        if (product is not null)
        {
            result = db.MesProductFrozens.Where(t => t.Barcode == barCode).Update(new POMesProductFrozen
            {
                Spec = productFrozenBean.Spec,
                Itnbr = productFrozenBean.Itnbr,
                Itdsc = productFrozenBean.Itdsc,
                Wdate = productFrozenBean.Wdate,
                Wtim = productFrozenBean.Wtime,

                Wsht = productFrozenBean.Wsht,
                Wban = productFrozenBean.Wban,
                Userid = productFrozenBean.UserId,

                Nooutrsn = productFrozenBean.Nooutrsn,
                Itmstatus = productFrozenBean.Itmstatus,
                Judguserid = productFrozenBean.Judguserid,
                Judgdt = productFrozenBean.Judgdt,
                RcvFlag = productFrozenBean.RcvFlag,
                RcvDt = productFrozenBean.RcvDt,
                EntUserId = productFrozenBean.EntUserId,
                EntDt = productFrozenBean.EntDt,
                UpdUserId = productFrozenBean.UpdUserId,
                UpdDt = productFrozenBean.UpdDt,
            });
            db.SaveChanges();
        }
        else
        {
            result = db.MesProductFrozens.Insert(new POMesProductFrozen()
            {
                Id = XID.Nextval,
                Barcode = productFrozenBean.Barcode,
                Spec = productFrozenBean.Spec,
                Itnbr = productFrozenBean.Itnbr,
                Itdsc = productFrozenBean.Itdsc,
                Wdate = productFrozenBean.Wdate,
                Wtim = productFrozenBean.Wtime,
                Wsht = productFrozenBean.Wsht,
                Wban = productFrozenBean.Wban,
                Userid = productFrozenBean.UserId,
                Nooutrsn = productFrozenBean.Nooutrsn,
                Itmstatus = productFrozenBean.Itmstatus,
                Judguserid = productFrozenBean.Judguserid,
                Judgdt = productFrozenBean.Judgdt,
                RcvFlag = productFrozenBean.RcvFlag,
                RcvDt = productFrozenBean.RcvDt,
                EntUserId = productFrozenBean.EntUserId,
                EntDt = productFrozenBean.EntDt,
                UpdUserId = productFrozenBean.UpdUserId,
                UpdDt = productFrozenBean.UpdDt,
            });
        }


        if (result == 0)
        {
            return 0;
        }

        var productCode = productFrozenBean.Barcode;
        var status = productFrozenBean.Itmstatus;
        db.ProdProducts.Where(t => t.ProductCode == productCode).Update(new POProdProduct
        {
            Frozen = status == "Y" ? 1 : 0,
            UpdateTime = DateTime.Now,
        });
        return db.SaveChanges();
    }

    public void UpdateProductMateiralId(string barCode, long mateiralId)
    {
        var db = Db.Default;

        var product = db.ProdProducts.Where(t => t.ProductCode == barCode)
            .Update(new POProdProduct()
            {
                UpdateTime = DateTime.Now,
                MaterialId = mateiralId
            });
        db.SaveChanges();
    }
    public int GetStockProductNum()
    {
        var db = Db.Default;
        var result = db.WcsLocSkus.Join(db.RcsLocations, f => f.LocId, s => s.Id, (f, s) => s)
            .Where(t => t.LocType == (int)EnumLocType.Location && t.UseFlag == 1 && t.LocEnable == 1 && t.LocHasSku == 1).Count();
        return result;

    }

    public List<LocProductBean> GetLocProductBeans()
    {
        var db = Db.Default;

        var list = db.RcsLocations.Join(db.WcsLocSkus, f => f.Id, s => s.LocId, (f, s) => new
        {
            Location = f,
            LocSku = s,
        }).Join(db.ProdSkuProducts, f => f.LocSku.SkuId, s => s.SkuId, (f, s) => new
        {
            f.Location,
            ProdSkuProduct = s,
        }).Join(db.ProdProducts, f => f.ProdSkuProduct.ProductId, s => s.Id, (f, s) => new
        {
            f.Location,
            Product = s,
        }).Join(db.ProdMaterials, f => f.Product.MaterialId, s => s.Id, (f, s) => new
        {
            f.Location,
            f.Product,
            Material = s,
        }).Where(f => f.Location.LocType == (int)EnumLocType.Location)
        .Select(f => new LocProductBean
        {
            LocId = f.Location.Id,
            MaterialId = f.Product.MaterialId,
            MaterialCode = f.Material.MaterialCode,
            LocTaskOccupy = f.Location.TaskOccupy,
            LocEnable = f.Location.LocEnable,
            LocOutEnable = f.Location.OutEnable,
            LocHasSkuTime = f.Location.LocHasSkuTime,
            MdTime = f.Product.MdTime,
            LocGroupId = f.Location.GroupId,
            LocGroupIndex = f.Location.GroupIndex,
            ProductId = f.Product.Id,
            TireLoc = f.Product.TireLock,
        }).ToList();

        return list;
    }

}
